//
//  XLStatusViewModel.swift
//  SinaWeibo
//
//  Created by 李凯 on 16/9/6.
//  Copyright © 2016年 kevin. All rights reserved.
//

import UIKit

class XLStatusViewModel: NSObject {
    
    // 当前微博作者的会员图标
    var memberImage: UIImage?
    // 认证的图标
    var avatarImage: UIImage?
    // 转发数
    var reposts_count: String?
    // 评论数量的字符串
    var comments_count: String?
    // 静态数量的字符串
    var attitudes_count: String?
    /// 转发微博的内容
    var retweetStautsText: String?
    /// 格式化好的来源字符串
    var sourceString: String?
    /// 已经计算好的时间字符串
    var createAtString: String? {
        guard let createAt = self.status?.created_at else {
            return nil

    }
        
        let formatter = DateFormatter()
        // 获取到当前的日历，日历对象有很多判断时间的方法
        let calendar = NSCalendar.current
        // 准备一个当前时间
        let currentDate = Date()
        
        if isDateInThisYear(target: createAt) {
            // 是否今天
            if calendar.isDateInToday(createAt) {
                // 是今天
                // 先求出当前时间与微博创建时间的秒数差值
                let result = currentDate.timeIntervalSince(createAt)
                print(result)
                if result < 60 {
                    return "刚刚"
                }else if result < 3600 {
                    return "\(Int(result) / 60)分钟前"
                }else {
                    return "\(Int(result) / 3600)小时前"
                }
            }else if calendar.isDateInYesterday(createAt) {
                // 是昨天
                formatter.dateFormat = "昨天 HH:mm"
                return formatter.string(from: createAt)
            }else {
                // 其他
                formatter.dateFormat = "MM-dd HH:mm"
                return formatter.string(from: createAt)
            }
        }else{
            // 不是今年
            formatter.dateFormat = "yyyy-MM-dd"
            return formatter.string(from: createAt)
        }
    }
    
    var status: XLStatus? {
        didSet {
            // 会员图标计算出来
            if let mbrank = status?.user?.mbrank, mbrank > 0 {
                // 会员图标名字
                let imageName = "common_icon_membership_level\(mbrank)"
                memberImage = UIImage(named: imageName)
            }
            
            // 认证图标计算 认证类型 -1：没有认证，1：认证用户，2,3,5: 企业认证，220: 达人
            if let type = status?.user?.verified_type {
                switch type {
                case 1: // 大v
                    avatarImage = #imageLiteral(resourceName: "avatar_vip")
                case 2, 3, 5: // 企业
                    avatarImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
                case 220: // 达人
                    avatarImage = #imageLiteral(resourceName: "avatar_grassroot")
                default:
                    break
                }
            }
            // 计算转发，评论，赞数
            reposts_count = caclCount(count: status?.reposts_count ?? 0, defaultTitle: "转发")
            comments_count = caclCount(count: status?.comments_count ?? 0, defaultTitle: "评论")
            attitudes_count = caclCount(count: status?.attitudes_count ?? 0, defaultTitle: "赞")
            // 计算转发微博的内容
            if let text = status?.retweeted_status?.text, let name = status?.retweeted_status?.user?.name{
                retweetStautsText = "@\(name):\(text)"
            }
            // <a href=\"http://app.weibo.com/t/feed/3722nD\" rel=\"nofollow\">微博树洞</a>
            // 来自 微博树洞
            if let source = status?.source{
                if let preRange = source.range(of: "\">"), let sufRange = source.range(of: "</") {
                    let result = source.substring(with: preRange.upperBound..<sufRange.lowerBound)
                    sourceString = "来自\(result)"
                }
            }
        }
    }
    
 
    private func isDateInThisYear(target: Date) -> Bool {
        
        let currentDate = Date()
        
        // 取到这两个时间的年份字符串
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy"
        
        // 取到目标日期的年份
        let targetYear = formatter.string(from: target)
        let currentYear = formatter.string(from: currentDate)
        
        return targetYear == currentYear
    }

 
    private func caclCount(count: Int, defaultTitle: String) -> String {
        if count == 0 {
            return defaultTitle
        }else{
            // 0 - 10000 --> 显示具体的数值
            // 10000-11000 --> 1万
            // 13000 --> 1.3万
            
            // 20800 2万
            if count < 10000 {
                return "\(count)"
            }else{
                // 取到十位数
                let result = count / 1000
                // 取到小数点后的一位
                let str = "\(Float(result) / 10)"
                let string = "\(str)万"
                // 如果有.0就替换成空字符串
                return string.replacingOccurrences(of: ".0", with: "")
            }
        }
    }



}
